package org.top10.parser;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.top10.dao.DBServerImpl;
import org.top10.entity.CollectEntity;
import org.top10.entity.CollectRecord;
import org.top10.entity.WebSite;

public abstract class Task implements Runnable {
	protected List<TOPParserImpl> PARSER ;//任务列表
	protected WebSite webSite;//网站标识
	protected String encoding;//编码
	public Task(int siteID,String encoding){
		PARSER = new ArrayList<TOPParserImpl>();
		this.encoding=encoding;
		//测试的时候注掉，用的时候去掉注解
		//webSite = DBServerImpl.getDbServer().getWebSiteByIdentifier(siteID);
	}
	public abstract void loadParser();
	public void run() {
		loadParser();
		Date d = new Date();
		
		for (TOPParserImpl p : PARSER) {
			CollectRecord record = new CollectRecord();
			record.setCaption(p.getCpation());
			record.setSite(webSite);
			record.setType(p.getType());
			record.setCollectDate(d);
			DBServerImpl.getDbServer().insert(record);
			List<CollectEntity> entitys = null;
			try {
				if(p.getEncoding()!=null){
					entitys = p.dealText(p.getEncoding());
				}else{
					entitys = p.dealText(encoding);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			DBServerImpl.getDbServer().inserts(entitys, record);//
		}
	}
}
